﻿using System;
using MyBuild.Messages;
using MyBuild.Notification;
using Rhino.Mocks;
using MbUnit.Framework;
using MyBuild.FileWatcher;

namespace MyBuild.Tests.FileWatcher
{
    [TestFixture]
    public class FileWatcherServiceFixture
    {
        private IBuildInitiator buildInitiator;
        private INotificationService notificationService;
        private ContinuousBuildServiceDetails details;

        [SetUp]
        public void SetUp()
        {
            details = new ContinuousBuildServiceDetails("", "*", "c:", "c:");
            buildInitiator = MockRepository.GenerateMock<IBuildInitiator>();
            notificationService = MockRepository.GenerateMock<INotificationService>();
        }
        [TearDown]
        public void TearDown()
        {
            buildInitiator.VerifyAllExpectations();
            notificationService.VerifyAllExpectations();
        }

        [Test]
        public void ctor_WithNullDetails_ThrowNullArgException()
        {
            try
            {
                new FileWatcherService(null, buildInitiator, notificationService);
            }
            catch (ArgumentNullException)
            {
                return;
            }
            Assert.Fail("ArgumentNullException expected");
        }
        [Test]
        public void ctor_WithNullBuildInitiator_ThrowNullArgException()
        {
            try
            {
                new FileWatcherService(new ContinuousBuildServiceDetails("", "", "", ""), null, notificationService);
            }
            catch (ArgumentNullException)
            {
                return;
            }
            Assert.Fail("ArgumentNullException expected");
        }
        [Test]
        public void ctor_WithNullINotificationService_ThrowNullArgException()
        {
            try
            {
                new FileWatcherService(new ContinuousBuildServiceDetails("", "", "", ""), buildInitiator, null);
            }
            catch (ArgumentNullException)
            {
                return;
            }
            Assert.Fail("ArgumentNullException expected");
        }
        [Test]
        public void ctor_WillSetUpBuildInititator_WithDetails()
        {
            //Arrange
            buildInitiator.Expect(bi => bi.Setup(Arg <ContinuousBuildServiceDetails>.Is.Equal(details), Arg<Action<ResultNotification>>.Is.Anything));
            //Act
            new FileWatcherService(details, buildInitiator, notificationService);
            //Assert - verify in tear down
        }

        [Test]
        public void FileModified_CallsBuildInitiator_Build()
        {
            //Arrange
            var fws = CreateFileWatcher();
            buildInitiator.Expect(bi => bi.Build());
            //Act
            fws.FileModified(null, null);
            //Assert - Verify in teardown
        }

        [Test]
        public void BuildResponse_CallsNotifictionSuccess_WhenBuildSucceeds()
        {
            //Arrange
            var successNotification = new SuccessNotification(details);
            var fws = CreateFileWatcher();
            notificationService.Expect(ns => ns.Notify(successNotification));
            //Act
            fws.BuildResponse(successNotification);
            //Assert - Verify in teardown
        }

        private FileWatcherService CreateFileWatcher()
        {
            return new FileWatcherService(details, buildInitiator, notificationService);
        }

    }
}